package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.SysAssetCategory;
import com.ruoyi.system.domain.vo.CategoryTreeSelect;
import com.ruoyi.system.mapper.SysAssetCategoryMapper;
import com.ruoyi.system.service.ISysAssetCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: ZLZ
 * Date: 2025/2/26
 */
@Service
public class SysAssetCategoryServiceImpl implements ISysAssetCategoryService {
    @Autowired
    private SysAssetCategoryMapper sysAssetCategoryMapper;

    /**
     * 查询分类列表
     * @param sysAssetCategory
     * @return
     */
    @Override
    public List<SysAssetCategory> selectSysAssetCategoryList(SysAssetCategory sysAssetCategory) {
        return sysAssetCategoryMapper.selectSysAssetCategoryList(sysAssetCategory);
    }

    /**
     * 查询资产分类
     *
     * @param categoryId 资产分类主键
     * @return 资产分类
     */
    @Override
    public SysAssetCategory selectSysAssetCategoryByCategoryId(Long categoryId)
    {
        return sysAssetCategoryMapper.selectSysAssetCategoryByCategoryId(categoryId);
    }

    /**
     * 新增资产分类
     *
     * @param sysAssetCategory 资产分类
     * @return 结果
     */
    @Override
    public int insertSysAssetCategory(SysAssetCategory sysAssetCategory)
    {
        sysAssetCategory.setCreateTime(DateUtils.getNowDate());
        return sysAssetCategoryMapper.insertSysAssetCategory(sysAssetCategory);
    }

    /**
     * 修改资产分类
     *
     * @param sysAssetCategory 资产分类
     * @return 结果
     */
    @Override
    public int updateSysAssetCategory(SysAssetCategory sysAssetCategory)
    {
        return sysAssetCategoryMapper.updateSysAssetCategory(sysAssetCategory);
    }

    /**
     * 批量删除资产分类
     *
     * @param categoryIds 需要删除的资产分类主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetCategoryByCategoryIds(Long[] categoryIds)
    {
        return sysAssetCategoryMapper.deleteSysAssetCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除资产分类信息
     *
     * @param categoryId 资产分类主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetCategoryByCategoryId(Long categoryId)
    {
        return sysAssetCategoryMapper.deleteSysAssetCategoryByCategoryId(categoryId);
    }

    /***
     * 查询当前节点是否有子分类
     * @param categoryId
     * @return
     */
    @Override
    public int selectListByParentId(Long categoryId) {
        return sysAssetCategoryMapper.selectListByParentId(categoryId);
    }

    /**
     * 校验分类名称是否唯一
     * @param categoryName
     * @return
     */
    @Override
    public int checkCategoryNameUnique(String categoryName) {
        return sysAssetCategoryMapper.checkCategoryNameUnique(categoryName);
    }

    /**
     * 查询分类树结构信息
     *
     * @param category 分类信息
     * @return 分类树信息集合
     */
    @Override
    public List<CategoryTreeSelect> selectCategoryTreeList(SysAssetCategory category)
    {
        List<SysAssetCategory> categories = selectSysAssetCategoryList(category);
        return buildCategoryCategoryTreeSelect(categories);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param categories 分类列表
     * @return 树结构列表
     */
    @Override
    public List<SysAssetCategory> buildCategoryTree(List<SysAssetCategory> categories)
    {
        List<SysAssetCategory> returnList = new ArrayList<>();
        List<Long> tempList = categories.stream().map(SysAssetCategory::getCategoryId).collect(Collectors.toList());
        for (SysAssetCategory category : categories)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categories, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categories;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param categories 分类列表
     * @return 下拉树结构列表
     */
    @Override
    public List<CategoryTreeSelect> buildCategoryCategoryTreeSelect(List<SysAssetCategory> categories)
    {
        List<SysAssetCategory> categoryTree = buildCategoryTree(categories);
        return categoryTree.stream().map(CategoryTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysAssetCategory> list, SysAssetCategory t)
    {
        // 得到子节点列表
        List<SysAssetCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysAssetCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<SysAssetCategory> getChildList(List<SysAssetCategory> list, SysAssetCategory t)
    {
        List<SysAssetCategory> tlist = new ArrayList<>();
        Iterator<SysAssetCategory> it = list.iterator();
        while (it.hasNext())
        {
            SysAssetCategory n = (SysAssetCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysAssetCategory> list, SysAssetCategory t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
